import { DeviceStorage } from "../utils/DeviceStorage";
import { BASE_URL, BASE_URL2 } from "../config";
import {i18n} from "../utils/locales/i18n";

let lang = 'tw'
DeviceStorage.get('lang').then(language => {
    lang = language
})

function _baseMixins(base) {
    return function (api) {
        return function (api2) {
            return base + api + api2
        }
    }
}

const usersMixins = _baseMixins(BASE_URL)('/v1/users')
const usersMixins2 = _baseMixins(BASE_URL2)('/v1/users')
const adMixins2 = _baseMixins(BASE_URL2)('/v1/ad')
const txMixins2 = _baseMixins(BASE_URL2)('/v1/tx/')
const baseMixins = _baseMixins(BASE_URL)('')


const api = {

    // 获取资产详情
    getAssets: usersMixins2('/getAssets'),
    // 绑定邮箱
    bindEmail: usersMixins2('/bindEmail'),
    // 绑定手机
    bindPhone: usersMixins2('/bindPhone'),
    // 绑定谷歌两步验证
    bindGoogle: usersMixins2('/bindGa'),
    // 修改登录密码
    modifyLoginPassword: usersMixins2('/modifyLogPass'),
    // 找回密码
    resetPassword: usersMixins('/resetPassword'),
    // 绑定银行卡
    uploadBank: usersMixins2('/uploadBank'),
    // 删除银行卡
    deleteBank: usersMixins2('/deleteBank'),
    // 获取订单列表
    getOrderList: txMixins2('/lists'),
    // 获取广告手续费
    getAdFee: adMixins2('/fee'),
    // 获取广告最大最小限额
    getAdLimit: adMixins2('/limit'),
    // 获取银行卡列表
    getBank: adMixins2('/bank'),
    // 获取市价
    getMarketPrice: adMixins2('/marketPrice'),
    // 发布hkd广告
    publishAd: adMixins2('/publishHkd'),
    // 获取广告记录
    getAdRecord: adMixins2('/record'),
    // 修改广告
    modifyAd: adMixins2('/modify'),
    // 更新广告状态
    updateAd: adMixins2('/update'),
    // 删除广告
    deleteAd: adMixins2('/delete'),
    // 购买下单
    placeOrder: adMixins2('/placeHkd'),
    // 获取下单详情
    getOrderDetails: txMixins2('/details'),
    // 充币地址获取
    getRechargeAddress: usersMixins2('/getRechargeAddress'),
    // 充币请求提交
    submitRecharge: usersMixins2('/submitRechargeOrder'),
    // 收币地址获取
    getReceiptAddress: usersMixins2('/getReceiptAddress'),
    // 添加充币地址
    newRechargeAddress: usersMixins2('/newRechargeAddress'),
    // 删除充币地址
    deleteRechargeAddress: usersMixins2('/deleteRechargeAddress'),
    // 获取提币限额和手续费
    getWithdrawLimitAndFee: usersMixins2('/getWithdrawLimitAndFee'),
    // 获取提币地址
    getWithdrawAddress: usersMixins2('/getWithdrawAddress'),
    // 提交提币工单
    submitWithdrawOrder: usersMixins2('/submitWithdrawOrder'),
    // 添加提币地址
    newWithdrawAddress: usersMixins2('/newWithdrawAddress'),
    // 更新提币地址
    editWithdrawAddress: usersMixins2('/editWithdrawAddress'),
    // 获取充币记录
    getRechargeRecord: usersMixins2('/getRechargeRecord'),
    // 获取提币记录
    getWithdrawRecord: usersMixins2('/getWithdrawRecord')
}

export class API {
    constructor() {

    }
    // 手机注册
    static async phoneRegister(params) {
        const res = await _post(usersMixins('/phoneRegister'), params)
        return res
    }
    // 邮箱注册
    static async emailRegister(params) {
        const res = await _post(usersMixins('/emailRegister'), params)
        return res
    }
    // 手机有效验证
    static async checkPhone(params) {
        const res = await _post(usersMixins('/checkPhone'), params)
        return res
    }
    // 邮箱有效验证
    static async checkEmail(params) {
        const res = await _post(usersMixins('/checkEmail'), params)
        return res
    }
    // 用户名有效验证
    static async checkUsername(params) {
        const res = await _post(usersMixins('/checkUsername'), params)
        return res
    }
    // 根据code获取jwt
    static async getJwt(code) {
        const res = await _get(usersMixins2('/loginCallback?code=' + code))
        return res
    }
    // 手机登录
    static async phoneLogin(params) {
        const res = await _post(usersMixins('/phoneLogin'), params)
        return res
    }
    // 邮箱登录
    static async emailLogin(params) {
        const res = await _post(usersMixins('/emailLogin'), params)
        return res
    }
    // 登录提交谷歌两步验证
    static async validateGoogle(params) {
        const res = await _post(usersMixins('/validateGoogle'), params)
        return res
    }
    // 获取广告列表
    static async getAdList(params) {
        // {"type":1,"sellToken":"fcc","buyToken":"eth"} // 1购买 2出售
        const res = await _post(adMixins2('/lists'), params)
        return res
    }
    // 获取用户信息
    static async getUserInfo() {
        const res = await _get(usersMixins2('/getUserinfo'))
        return res
    }

    // 上传身份证kyc
    static async uploadIdCard(formData) {
        const res = await _post_form(usersMixins2('/uploadIdCard'), formData)
        return res
    }

    // 上传护照kyc
    static async uploadPassport(formData) {
        const res = await _post_form(usersMixins2('/uploadPassport'), formData)
        return res
    }

    // 上面这样写有点麻烦，再抽象一下。。。。
    static async _fetch(routeName, params = {}, method = 'POST') {
        let res
        if (method === 'POST') {
            res = await _post(api[routeName], params)
        } else {
            res = await _get(api[routeName])
        }
        return res
    }

}

async function _post(api, data = {}) {
    const jwt = await DeviceStorage.get('jwt')
    const options = {
        headers: {
            'Content-Type': 'application/json;text/plain',
            'Accept': 'application/json',
            'Accept-Language': lang
        },
        method: 'POST',
        body: JSON.stringify(data)
    }
    if (jwt) {
        options.headers.Authorization = 'Bearer ' + jwt
    }
    try {
        const res = await fetch(api, options)
        const resJson = res.json()
        return resJson
    } catch (e) {
        console.log('POST ERROR: ', e);
    }
}

async function _post_form(api, formData) {
    const jwt = await DeviceStorage.get('jwt')
    const options = {
        headers: {
            'Accept': 'application/json',
            'Content-Type':'multipart/form-data',
            'Accept-Language': lang
        },
        method: 'POST',
        body: formData
    }
    if (jwt) {
        options.headers.Authorization = 'Bearer ' + jwt
    }
    try {
        const res = await fetch(api, options)
        const resJson = res.json()
        return resJson
    } catch (e) {
        console.log('POST ERROR: ', e);
    }
}

async function _get(api) {
    const jwt = await DeviceStorage.get('jwt')
    const options = {
        method: 'GET'
    }
    if (jwt) {
        options.headers = {
            'Authorization': 'Bearer ' + jwt,
            'Accept-Language': lang
        }
    }
    try {
        const res = await fetch(api, options)
        const resJson = res.json()
        return resJson
    } catch (e) {
        console.log('POST ERROR: ', e);
    }
}
